package com.example.proyectoformulas;

import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


public class ContenidoFormula extends Fragment {
	public static final String PREFERENCES = "preferences";
	
	Context context = null;
	private View parentView;
	private SharedPreferences preferences = null;
	private SharedPreferences.Editor prefEditor = null;
	private ListView listView;
	
/*	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		main2 mainActivity = (main2) activity;
		//items = mainActivity.getListadoFormulas();
		context = activity;
	}*/
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(Fragment3.compartir != null){
        	//invisible
        	Fragment3.compartir.setVisibility(0);
        }
		
		context = getActivity();

		//setRetainInstance(true);
		//setHasOptionsMenu(true);
		
		preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
		prefEditor = preferences.edit();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		context = getActivity();
		
		parentView = inflater.inflate(R.layout.contenido_formula, container, false);
		
		return parentView;
	
	}
	
	@Override
    public void onDestroyView() {
        super.onDestroyView();
        if(Fragment3.compartir != null){
        	//invisible
        	Fragment3.compartir.setVisibility(4);
        }
        
        if (TabUltimos.listView != null){ 
	        if (!TabUltimos.listView.isEnabled()){        	
	        	TabUltimos.listView.setEnabled(true);
	        	
	        
	        	
	        	//FragmentTransaction ft  = getFragmentManager().beginTransaction();
    			getFragmentManager().popBackStack(getFragmentManager().getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    			
    			
    			Fragment fragment = new TabUltimos();
        		FragmentTransaction ft2  = getFragmentManager().beginTransaction();
        		ft2.addToBackStack(null);
        		ft2.add(R.id.formulasContent,fragment);
	       	    ft2.commit();
	        }
        }
        if(FragListadoFormulas.listView != null){
	        if(!FragListadoFormulas.listView.isEnabled()){
	        	FragListadoFormulas.listView.setEnabled(true);	
	        }
        }
        
       /*FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        Fragment newFragment = this;
        this.onDestroy();
        ft.remove(this);
        ft.replace(R.id.formulasContent,newFragment);
         //container is the ViewGroup of current fragment
        ft.addToBackStack(null);   
        ft.commit();*/
	}
	
	
	

}
